﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace PaiBanDaPing
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        XDocument doc = new XDocument();
        string autoupdateFile = "autoupdate.xml";

        public MainWindow()
        {
            InitializeComponent();
            InitXML();
            this.Loaded += MainWindow_Loaded;
            this.Closing += MainWindow_Closing;
        }

        private void InitXML()
        {
            if (File.Exists(autoupdateFile))
            {
                try
                {
                    doc = XDocument.Load(autoupdateFile);
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show(ex.Message);
                }
            }
            else
                System.Windows.MessageBox.Show(autoupdateFile + "不存在");
        }

        private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                foreach (var item in ScreenManger.DisplayWindows.Values)
                {
                    item.Close();
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.ToString());
            }
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            if (System.Windows.Forms.Screen.AllScreens.Count() == 1)
            {
                string url = doc.Root.Element("DaPing").Element("Url").Value;
                CustomScreenModel item = new CustomScreenModel();
                item.Url = url;
                DaPingDisplayWin win = new DaPingDisplayWin();
                win.DisplayStyle = 2;
                win.Show();
                win.DisPlay(item);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.Button btn = sender as System.Windows.Controls.Button;
            string content = btn.Content.ToString();
            if (content.Equals("确定"))
            {
                if (txtName.Text.Equals("admin") && txtPwd.Text.Equals("bochuang"))
                {
                    btnYC.Visibility = Visibility.Visible;
                    _screenListCtrl.SetUrlCtrlVisible(true);
                }
                else
                {
                    btnYC.Visibility = Visibility.Collapsed;
                    _screenListCtrl.SetUrlCtrlVisible(false);
                }
            }
            else
            {
                btnYC.Visibility = Visibility.Collapsed;
                _screenListCtrl.SetUrlCtrlVisible(false);
                txtName.Text = "";
                txtPwd.Text = "";
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string url = doc.Root.Element("DaPing").Element("Url").Value;
            CustomScreenModel item = new CustomScreenModel();
            item.Url = url;
            DaPingDisplayWin win = new DaPingDisplayWin();
            win.DisplayStyle = 1;
            win.Show();
            win.DisPlay(item);
        }
    }
}
